<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/19
 * Time: 11:48
 */

namespace app\index\controller;
use think\Db;
use think\Exception;
use think\Loader;
use think\Request;
use think\Response;
class ServiceOrder extends Base
{
    /**
     * 码卷验证
     */
    public function check_code($order_number){

        $data = Db::name('order')->where('order_type',2)->where('order_number|shop_order_number',$order_number)->update(['code' =>2,'code_time' => date('Y-m-d H:i:s')]);

        if($data !== false){
            $str = '<script>alert("码卷验证成功")</script>';
           return response($str);
        }
        return 'fail';

    }


    public function qrcode($order_number)
    {
        Loader::import('phpqrcode.phpqrcode', EXTEND_PATH);
        $url = Request::instance()->domain().'/index.php/index/service_order/check_code/order_number/'.$order_number;
        $object = new \QRcode();
        $path = ROOT_PATH . 'public/index/service/' . $order_number . '.png';
        //第二个参数false的意思是不生成图片文件，如果你写上‘picture.png’则会在根目录下生成一个png格式的图片文件
        $ret = $object->png($url, false, 10, 10, 2);
        exit;
    }

    public function type()
    {
        //$result = Db::query('call protect_type()');
        $result = Db::name('protect_type')->select();
        return ['code' => '200', 'message' => '保单类型', 'data' => $result];
    }

    public function evaluate_create($member_id, $order_number, $content, $single_id, $shop_id = 0)
    {
        try {
            if ($shop_id == 0) {
                $shop_id = Db::name('order')->where('order_number', $order_number)->value('shop_id');
            }
            $order = Db::name('order')->where('order_number', $order_number)->find();

            $parent_id = 0;
            if ($order['order_type'] == 3) {
                $parent_id = $single_id;//如果是礼包 父级和子集分类相同
            }
            if ($order['order_type'] == 2) {
                $parent_id = Db::name('single')->where('id', $single_id)->value('parent_id');
            }

            $pic_url = uploadOne('pic_url');

            $insert = [
                'order_type' => $order['order_type'],
                'member_id' => $member_id,
                'order_number' => $order_number,
                'content' => $content,
                'pic_url' => $pic_url,
                'single_id' => $single_id,
                'shop_id' => $shop_id,
                'parent_id' => $parent_id,
                'create_time' => date('Y-m-d H:i:s', time())
            ];
            $data = Db::name('order_evaluate')->insert($insert);
            if ($data) {
                $ret = Db::name('order')->where('order_number|shop_order_number', $order_number)->where('status', 5)->setField('status', 4);
                if ($ret) {
                    return ['code' => 200, 'message' => '评价成功'];
                }
            }
            return ['code' => 400, 'message' => '失败'];
        } catch (\Exception $e) {
            return $e->getMessage();
        }

    }


//    public function evaluate_index($order_number,$member_id){
//        $single_id = Db::name('order')->where('order_number',$order_number)->where('member_id',$member_id)->value('single_id');
//        $single_title = Db::name('single')->where('id',$single_id)->value('cat_name');
//        $single_pic_url = Db::name('single')->where('id',$single_id)->value('pic_url');
//        return ['code' => 200 ,'message' => '订单评价展示数据',];
//    }

    /**
     * @param $member_id
     * @param $type_id_str
     * @param $protecteder
     * @return array
     * 汽车保险
     */
    public function protect($shop_id = 0,$member_id,$type_id_str,$protecteder,$phone,$single_id,$plat_number,$plat_name,$engine_number,$brand_number,$plat_code,$register_time)
    {

        Db::startTrans();
        try {
            $protecter_face = uploadOne('protecter_face');
            $protecter_back = uploadOne('protecter_back');
            $car_face = uploadOne('car_face');
            $car_back = uploadOne('car_back');
            $order_number = order_number();
            $shop_order_number = shop_order_number($member_id);
            $time = date('Y-m-d H:i:s', time());
            $username = Db::name('member')->where('id', $member_id)->value('username');
            $single_parent_id = Db::name('single')->where('id', $single_id)->value('parent_id');
            $single_title  = Db::name('single')->where('id', $single_parent_id)->value('cat_name');
            $amount = Db::name('shop_service_price')->where('single_id', $single_id)->where('shop_id', $shop_id)->value('price');
            if(is_null($amount) || empty($amount) || $amount == '0.00'){
                return ['code' => 400,'message' => '此门店服务金额未设置,请联系客服'];
            }
            if($single_parent_id == 1){ //汽车保险 就是等待沟通
                $status = 50;
            }else{
                $status = 1;
            }
            $order = [
                'shop_id' => $shop_id,
                'shipping' => 2,
                'order_number' => $order_number,
                'shop_order_number' => $shop_order_number,
                'order_type' => 2,
                'service_type_id' => 1,//1保险2年检3
                'single_id' => $single_id,
                'single_parent_id' => $single_parent_id,
                'per' => Db::name('single')->where('id',$single_id)->value('per'),
                'single_title' => $single_title,
                'member_id' => $member_id,
                'username' => $username,
                'amount' => $amount,
                'order_price' => $amount,
                'pay_way' => '-',
//                'bill' => $bill,//是否开发票
//                'bill_type' => $bill_type,//是否开发票
//                'bill_header' => $bill_header,//是否开发票
//                'bill_content' => $bill_content,//是否开发票
//                'bill_email' => $bill_email,//是否开发票
//                'bill_phone' => $bill_phone,
                'status' => $status,//未支付-待支付
                'create_time' => date('Y-m-d H:i:s', time())
            ];
            $order_id = Db::name('order')->insertGetId($order);
            $protect = [
                'plat_number' => $plat_number,
                'plat_name' => $plat_name,
                'engine_number' => $engine_number,
                'brand_number' => $brand_number,
                'plat_code' => $plat_code,
                'register_time' => $register_time,
                'order_id' => $order_id,
                'order_number' => $order_number,
                'shop_order_number' => $shop_order_number,
                'type_id_str' => $type_id_str,
                'phone' => $phone,
                'protecteder' => $protecteder,
                'protecter_back' => $protecter_back,
                'protecter_face' => $protecter_face,
                'car_face' => $car_face,
                'car_back' => $car_back,
                'create_time' => $time,
            ];
            $protect_id = Db::name('order_service')->insertGetId($protect);
            Db::commit();
            return ['code' => 200,'message' => '保单提交成功','order_number' => $order_number];
        } catch (Exception $e) {
            Db::rollback();
            return ['code' => $e->getCode(),'message' => $e->getMessage(),'line' => $e->getLine()];
        }

    }

    /**
     * @param $member_id
     * @param $protecteder
     * @param $phone
     * @return array
     * 汽车年检
     */
    public function year_check($shop_id = 0,$member_id,$protecteder,$phone,$single_id,$plat_number,$plat_name,$engine_number,$brand_number,$plat_code,$register_time){
        Db::startTrans();
        try {
            $protecter_face = uploadOne('protecter_face');
            $protecter_back = uploadOne('protecter_back');
            $car_face = uploadOne('car_face');
            $car_back = uploadOne('car_back');
            $order_number = order_number();
            $shop_order_number = shop_order_number($member_id);
            $time = date('Y-m-d H:i:s', time());
            $username = Db::name('member')->where('id', $member_id)->value('username');
            $single_parent_id = Db::name('single')->where('id', $single_id)->value('parent_id');
            $single_title  = Db::name('single')->where('id', $single_parent_id)->value('cat_name');
            $amount = Db::name('shop_service_price')->where('single_id', $single_id)->where('shop_id', $shop_id)->value('price');
            if(is_null($amount) || empty($amount) || $amount == '0.00'){
                return ['code' => 400,'message' => '此门店服务金额未设置,请联系客服'];
            }
            $order = [
                'shop_id' => $shop_id,
                'shipping' => 2,
                'order_number' => $order_number,
                'shop_order_number' => $shop_order_number,
                'order_type' => 2,
                'service_type_id' => 2,//1保险2年检3直接提交 4是url汽车超市
                'single_id' => $single_id,
                'single_parent_id' => $single_parent_id,
                'per' => Db::name('single')->where('id',$single_id)->value('per'),
                'single_title' => $single_title,
                'member_id' => $member_id,
                'username' => $username,
                'amount' => $amount,
                'order_price' => $amount,
                'pay_way' => '-',
                'bill' => 2,//不开发票
                'status' => 1,//未支付-待支付
                'create_time' => date('Y-m-d H:i:s', time())
            ];
            $order_id = Db::name('order')->insertGetId($order);
            $protect = [
                'plat_number' => $plat_number,
                'plat_name' => $plat_name,
                'engine_number' => $engine_number,
                'brand_number' => $brand_number,
                'plat_code' => $plat_code,
                'register_time' => $register_time,
                'order_id' => $order_id,
                'order_number' => $order_number,
                'shop_order_number' => $shop_order_number,
                'phone' => $phone,
                'protecteder' => $protecteder,
                'protecter_back' => $protecter_back,
                'protecter_face' => $protecter_face,
                'car_face' => $car_face,
                'car_back' => $car_back,
                'create_time' => $time,
            ];
            $protect_id = Db::name('order_service')->insertGetId($protect);
            Db::commit();
            return ['code' => 200,'message' => '年检订单提交成功','order_number' => $order_number];
        } catch (Exception $e) {
            Db::rollback();
            return ['code' => $e->getCode(),'message' => $e->getMessage(),'line' => $e->getLine()];
        }

    }

    /**
     * @param $order_number
     * @param $bill
     * @param $bill_phone
     * @param $bill
     * @param string $bill_type
     * @param string $bill_header
     * @param string $bill_content
     * @param string $bill_email
     * @return array
     * 是否开发票
     */

    public function bill($member_id,$order_number,$bill,$bill_phone,$bill_type = '',$bill_header = '',$bill_content = '',$bill_email = ''){
        try {
            $insert = [
                'bill' => $bill,//是否开发票
                'bill_type' => $bill_type,//是否开发票
                'bill_header' => $bill_header,//是否开发票
                'bill_content' => $bill_content,//是否开发票
                'bill_email' => $bill_email,//是否开发票
                'bill_phone' => $bill_phone,
            ];
            $ret = Db::name('order')->where('order_number|shop_order_number', $order_number)->where('member_id', $member_id)->update($insert);
            if ($ret !== false) {
                return ['code' => 200, 'message' => '订单发票信息修改成功', 'order_number' => $order_number];
            }
            return ['code' => 400, 'message' => '失败'];
        } catch (\Exception $e) {
            return ['message' => $e->getMessage()];
        }
    }


    public function remark($member_id,$order_number,$explain){

        $ret = Db::name('order')->where('order_number|shop_order_number', $order_number)->where('member_id', $member_id)->update(['explain' => $explain]);
        if ($ret !== false) {
            return ['code' => 200, 'message' => '修改备注信息成功', 'order_number' => $order_number];
        }
}

    /**
     * 洗车和轮胎
     */
    public function beauty_tires($shop_id = 0,$member_id,$single_id){
        Db::startTrans();
        try {
            $order_number = order_number();
            $shop_order_number = shop_order_number($member_id);
            $time = date('Y-m-d H:i:s', time());
            $username = Db::name('member')->where('id', $member_id)->value('username');
            $single_parent_id = Db::name('single')->where('id', $single_id)->value('parent_id');
            $single_title  = Db::name('single')->where('id', $single_parent_id)->value('cat_name');
            $amount = Db::name('shop_service_price')->where('single_id', $single_id)->where('shop_id', $shop_id)->value('price');


            if(is_null($amount) || empty($amount) || $amount =='0.00'){
                return ['code' => 400,'message' => '此门店服务金额未设置,请联系客服'];
            }
            $order = [
                'shop_id' => $shop_id,
                'shipping' => 2,
                'order_number' => $order_number,
                'shop_order_number' => $shop_order_number,
                'order_type' => 2,
                'service_type_id' => 3,//1保险2年检3 4个没有附表的分类
                'single_id' => $single_id,
                'single_parent_id' => $single_parent_id,
                'per' => Db::name('single')->where('id',$single_id)->value('per'),
                'single_title' => $single_title,
                'member_id' => $member_id,
                'username' => $username,
                'amount' => $amount,
                'order_price' => $amount,
                'pay_way' => '-',
                'bill' => 2,//不开发票
                'status' => 1,//未支付-待支付
                'create_time' => date('Y-m-d H:i:s', time())
            ];
            $order_id = Db::name('order')->insertGetId($order);
            Db::commit();
            return ['code' => 200,'message' => $single_title.'-订单提交成功','order_number' => $order_number];
        } catch (Exception $e) {
            Db::rollback();
            return ['code' => $e->getCode(),'message' => $e->getMessage(),'line' => $e->getLine()];
        }

    }


}